package com.starlight.flower_server.domain.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.starlight.flower_server.controller.exception.NotFoundException;
import com.starlight.flower_server.domain.TotalData;
import com.starlight.flower_server.domain.flowerKnowledge.data.FlowerKnowledgeData;
import com.starlight.flower_server.infrastructure.Status;
import com.starlight.flower_server.infrastructure.entity.FlowerKnowledge;
import com.starlight.flower_server.infrastructure.repository.FlowerKnowledgeRepository;
import com.starlight.flower_server.domain.service.FlowerKnowledgeService;
import com.starlight.flower_server.domain.flowerKnowledge.cqrs.FlowerKnowledgePageQuery;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author yfy2001
 * @date 2023/4/5 15:01
 */
@Service
public class FlowerKnowledgeServiceImpl implements FlowerKnowledgeService {

    @Resource
    private FlowerKnowledgeRepository flowerKnowledgeRepository;

    @Override
    public FlowerKnowledge queryFlowerKnowledgeDetailById(Long id) {
        FlowerKnowledge flowerKnowledge=flowerKnowledgeRepository.queryDetail(id);
        if (flowerKnowledge==null){
            throw new NotFoundException(Status.RECORD_NOT_FOUND.getMessage(),Status.RECORD_NOT_FOUND);
        }
        return flowerKnowledge;
    }

    /**
     * 实体类isDeleted字段有@TableLogic注解
     * 调用baseMapper删除方法时会自动变成逻辑删除
     * @param id
     * @return
     */
    @Override
    public Integer logicDeleteFlowerKnowledgeById(Long id) {
        Integer i=flowerKnowledgeRepository.logicDelete(id);
        if (i.equals(0)){
            throw new NotFoundException(Status.RECORD_NOT_FOUND.getMessage(),Status.RECORD_NOT_FOUND);
        }
        return i;
    }

    @Override
    public Integer insertFlowerKnowledge(FlowerKnowledge flowerKnowledge) {
        return flowerKnowledgeRepository.insert(flowerKnowledge);
    }

    @Override
    public Integer modifyFlowerKnowledge(FlowerKnowledge flowerKnowledge) {
        return flowerKnowledgeRepository.modify(flowerKnowledge);
    }


    @Override
    public IPage<FlowerKnowledgeData> queryFlowerKnowledgeList(FlowerKnowledgePageQuery query) {
        IPage<FlowerKnowledge> page=flowerKnowledgeRepository.queryPage(query);
        return page.convert(flowerKnowledge -> {
            FlowerKnowledgeData flowerKnowledgeData=new FlowerKnowledgeData();
            BeanUtils.copyProperties(flowerKnowledge,flowerKnowledgeData);
            return flowerKnowledgeData;
        });
    }

    @Override
    public TotalData<FlowerKnowledgeData> queryAllFlowerKnowledgeData() {
        TotalData<FlowerKnowledgeData> data=new TotalData<>();
        List<FlowerKnowledge> flowerKnowledgeList=flowerKnowledgeRepository.queryAll();
        List<FlowerKnowledgeData> flowerKnowledgeDataList=flowerKnowledgeList
                .stream().map(flowerKnowledge -> {
                    FlowerKnowledgeData flowerKnowledgeData=new FlowerKnowledgeData();
                    BeanUtils.copyProperties(flowerKnowledge,flowerKnowledgeData);
                    return flowerKnowledgeData;
                }).toList();
        Long total=(long) flowerKnowledgeDataList.size();
        data.setTable("FlowerKnowledge");
        data.setStatTime(new Date());
        data.setTotal(total);
        data.setRecords(flowerKnowledgeDataList);
        return data;
    }

    @Override
    public TotalData<FlowerKnowledgeData> queryFlowerKnowledgeTotalCount() {
        TotalData<FlowerKnowledgeData> data=new TotalData<>();
        Long total=flowerKnowledgeRepository.queryTotalCount();
        data.setTable("FlowerKnowledge");
        data.setStatTime(new Date());
        data.setTotal(total);
        return data;
    }

    @Override
    public List<FlowerKnowledgeData> queryRandomFlowerKnowledgeList(Integer count) {
        List<FlowerKnowledge> flowerKnowledgeList=flowerKnowledgeRepository.queryRandom(count);
        return flowerKnowledgeList
                .stream().map(flowerKnowledge -> {
                    FlowerKnowledgeData flowerKnowledgeData=new FlowerKnowledgeData();
                    BeanUtils.copyProperties(flowerKnowledge,flowerKnowledgeData);
                    return flowerKnowledgeData;
                }).toList();
    }


}
